<?php

/**
 * @author ducnhat
 * @email ddnhat@gmail.com
 * @copyright 2014
 */

class Admin_Form_ValidateSupplier{
	
    protected $_isError = false;
	protected $_messageError = null;
    protected $_arrData = null;
    protected $_classError = null;
    
    public function __construct($params = array(), $edit = false, $data = null){
        
        $translate = Zend_Registry::get('Zend_Translate');
        
        $validateName = new Zend_Validate();
        $validatePhoneNumber = new Zend_Validate();
        $validateAddress = new Zend_Validate();
        $validateDiscount = new Zend_Validate();
        
        $optionName = array('table' => 'suppliers', 'field' => 'name');
        
        if($edit){
            $optionName['exclude'] = array ('field' => 'id', 'value' => $params['id']);
        }
        
        $validateName->addValidator(new Zend_Validate_NotEmpty, true)
                        ->addValidator(new Zend_Validate_Alnum(true), true)
                        ->addValidator(new Zend_Validate_Db_NoRecordExists($optionName), true);
        
        $validatePhoneNumber->addValidator(new Zend_Validate_Digits(), true)
                            ->addValidator(new Zend_Validate_GreaterThan(0), true);
        
        $validateAddress->addValidator(new Zend_Validate_StringLength(array('max' => 150)), true);
        
        $validateDiscount->addValidator(new Zend_Validate_Digits(), true);
        
        /*
        * Check Name
        */
        
        if(!$validateName->isValid($params['name'])){
            $this->_messageError['name'] = '<label class="control-label" for="inputError">' . $translate->_('customer name') . current($validateName->getMessages()) . '</label>';
            $this->_arrData['name'] = '';
            $this->_classError['name'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['name'] = $data['name'];
            }
        }else{
            $this->_messageError['name'] = '';
            $this->_arrData['name'] = $params['name'];
            $this->_classError['name'] = '';
        }
        
        /*
        * Check Phone Number
        */
        
        $this->_messageError['phone_number'] = '';
        $this->_arrData['phone_number'] = $params['phone_number'];
        $this->_classError['phone_number'] = '';
                
        if(isset($params['phone_number']) && $params['phone_number'] != null){
            if(!$validatePhoneNumber->isValid($params['phone_number'])){
                $this->_messageError['phone_number'] = '<label class="control-label" for="inputError">' . $translate->_('phone number') . current($validatePhoneNumber->getMessages()) . '</label>';
                $this->_arrData['phone_number'] = '';
                $this->_classError['phone_number'] = 'has-error';
                $this->_isError = true;
                
                if($edit){
                    $this->_arrData['phone_number'] = $data['phone_number'];
                }
            }
        }
        
        /*
        * Check Address
        */
        
        if(!$validateAddress->isValid($params['address'])){
            $this->_messageError['address'] = '<label class="control-label" for="inputError">' . $translate->_('address') . current($validateAddress->getMessages()) . '</label>';
            $this->_arrData['address'] = '';
            $this->_classError['address'] = 'has-error';
            $this->_isError = true;
            
            if($edit){
                $this->_arrData['address'] = $data['address'];
            }
        }else{
            $this->_messageError['address'] = '';
            $this->_arrData['address'] = $params['address'];
            $this->_classError['address'] = '';
        }
        
        if($edit){
            $this->_arrData['id'] = $data['id'];
        }
    }
    
    public function isError(){
    	return $this->_isError;
    }
    
    public function getMessageError(){
        return $this->_messageError;
    }
    
    public function getClassError(){
    	return $this->_classError;
    }
    
    public function getData(){
        return $this->_arrData;
    }
}